classdef WaveHelper< handle
    properties (Constant)
        leaveOne = false %%要修改CalcAllErrors
    end
    methods(Static = true)
        % % Step1：把原始数据导出为mat文件
        function aa()
            
        end
        %%%2024-11-27 增加不同数据的删除操作
        function [y_test, x_test, inns] = GetInsFromPos(y_test, x_test, args)
            pos = load(args.fileSaveMaxPos, '-ascii');
            lenY = size(y_test, 2);
            rowY = size(y_test, 1);
            nRemove = args.nRemove;
            
            y_test_new = zeros(rowY - nRemove, lenY);
            XX = cell(3, 1);
            inns = cell(3, 1);
            for i = 1 : lenY
                inn = pos(1:nRemove, i);
                ins = true(rowY, 1);
                ins(inn) = false;
                
                XX{i} = x_test(ins, :);
                y_test_new(:, i) = y_test(ins, i);
                
                inns{i} = inn;
            end
            
            y_test = y_test_new;
            x_test = XX;
        end
        
        %%%2024-11-27 增加不同数据的删除操作
        function varargout = removeSamePoints(args, varargin)
            savePos = load(args.fileSaveMaxPos, '-ascii');
            
            nRemove = args.nRemove;
            
            pos = savePos(1:nRemove, 1:3);
            pos = unique(pos);
            
            szData = length(varargin);
            
            for k = 1 : szData
                y = varargin{k};
                
                zeroPos = true(size(y,1), 1);
                zeroPos(pos) = false;
                
                varargout{k} = y(zeroPos, :);
            end
            varargout{szData+1} = pos;
            
        end
        
        
        function [y_test, x_test, delpos] = DoCutTestData(nUseEachRemove, y_test, x_test, args)
            if nUseEachRemove == 1
                args.fileSaveMaxPos = fullfile(dataInputPath, 'pos-max-point-decrease.txt');
                [y_test, x_test, inns] = WaveHelper.GetInsFromPos(y_test, x_test, args);
                %%pos 做不到，不同pos
            elseif nUseEachRemove == 2
                args.fileSaveMaxPos = fullfile(dataInputPath, 'pos-max-point-decrease.txt');
                [y_test, x_test, delpos] = WaveHelper.removeSamePoints(args, y_test, x_test);
                testIdx(delpos) = false;
            end
        end
        
        %%%
        function savePos = FindAbnormalPoints(y_true, y_fit, args)
            [savePos, saveM] = Helper.FindMaxDiff(y_true, y_fit, args);%%测试集删点
% % %             [savePos, saveM] = WaveHelper.FindMaxDiff2(y_true, y_fit, args);%%测试集删点2025-02-07,最新测试集删点
            
            delpos = args.delpos;
            
            savePos = savePos(delpos, :);
            savePos = savePos(:)';
            fileSaveName = args.fileSaveTopPos;
            
            fileID = fopen(fileSaveName, 'w');
            fprintf(fileID, '%d,', savePos);
            fclose(fileID);
            %         save(fileSaveName, '-ascii', '-tabs', 'savePos');
        end        
% % % % % % % % % % % % % % % % % % % % % % % % % % 
% % % % %%联动的找异常点
        function savePos = FindAbnormalPoints2(y_true, y_fit, args)
            [savePos, saveM] = WaveHelper.FindMaxDiff2(y_true, y_fit, args);%%测试集删点2025-02-07,最新测试集删点
            
            delpos = args.delpos;
            
            savePos = savePos(delpos, :);
            savePos = savePos(:)';
            fileSaveName = args.fileSaveTopPos;
            
            fileID = fopen(fileSaveName, 'w');
            fprintf(fileID, '%d,', savePos);
            fclose(fileID);
            %         save(fileSaveName, '-ascii', '-tabs', 'savePos');
        end       


% % % % 测试集删点2025-02-07,改变循环，一次删3个
        function [savePos, saveM] = FindMaxDiff2(y_true, y_fit, args)                
            klen = args.klen;
            llen = size(y_true, 2);
            
            savePos = zeros(klen, llen);
            saveM = zeros(klen, llen);
            RR2 = zeros(klen, llen);            
            
            pos = true(length(y_true), 1);
            index = 1 : length(y_true);   
            for i = 1 : klen                
                
                for k = 1 : llen%%元素个数
% %                     y = y_true(:, k);
% %                     ypred = y_fit(:, k);
% %                 
% %                     y = y(pos);%%长度缩短了
% %                     ypred = ypred(pos);

                    y = y_true(pos, k);
                    ypred = y_fit(pos, k);%%长度缩短了
                    ind = index(pos);
                    
                    ymean = mean(y);                    

                    R2 = Helper.calcR2(y, ypred);
                    RR2(i, k) = R2;

% % % % % % %      OK：方法3,增加动态调整的均值
                    nnn = length(y);
                    SSR = sum((y - ymean).^2) * ones(size(y));
                    SSE = sum((ypred - y).^2) * ones(size(y));
                    
                    ss = (sum(y) - y) / (nnn - 1);
                    dd = y.^2 - nnn * ymean.^2 + (nnn - 1) * ss.^2;
                    eachSSR = SSR - dd;
                    eachSSE = SSE - (ypred - y).^2;
                    ratio = eachSSE ./ eachSSR;
                    [M, in] = min(ratio);
% % % % % % % % % % % % % % % % % % % % % % % % % % %   

                    savePos(i, k) = ind(in);
                    saveM(i, k) = M;
                end
                
                for k = 1 : llen%%设置选中标记
                    kin = savePos(i, k);
                    pos(kin) = false;
                end
            end         
            
            Helper.DrawDiffRR2(RR2)
            aa = [savePos, saveM];
            save(args.fileSaveMaxPos, '-ascii', '-tabs', 'aa');
            
            diff = RR2(2:end, :) - RR2(1:end-1, :);
            [B, I] = maxk(diff, 15);
            save(args.fileSaveDiffPos, '-ascii', '-tabs', 'I');
        end
        


  
        
    end
    
end